<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author FIRE
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="default" @click="handleExport()">
          <Icon icon="ant-design:download-outlined" /> {{ t('导出') }}
        </a-button>
        <!-- <a-button type="default" @click="handleImport()">
          <Icon icon="ant-design:upload-outlined" /> {{ t('导入') }}
        </a-button> -->
        <a-button type="primary" @click="handlekpsq({})" v-auth="'shop:order:cusFpApply:edit'">
          <Icon icon="fluent:add-12-filled" /> {{ t('发起开票申请') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.cusCode }}
        </a>
      </template>

      <template #fpUrl="{ record }">
        <a @click="handleopen(record.fpUrl)">
          {{ record.fpUrl }}
        </a>
      </template>
      <!-- v-if="hasPermission(['shop:order:cusFpApply:edit'])" -->
      <template #uploadFile="{ record }">
        <BasicUpload
          v-model:value="record.dataMap"
          disabled
          :bizKey="record.id"
          :bizType="'cusFpApply_file'"
          :uploadType="'file'"
          :loadTime="record.__t"
          :showPreview="true"
          :emptyHidePreview="true"
        />
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <FormImport @register="registerImportModal" @success="handleSuccess" />
    <BpmRuntimeTrace @register="registerTraceModal" />

    <!-- 发起开票申请 -->
    <BasicModal
      v-bind="$attrs"
      @register="registerModalkpsq"
      title="发起开票申请"
      @ok="handleModalkpsq"
    >
      <BasicForm @register="registerFormkpsq" />
    </BasicModal>
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsShopOrderCusFpApplyList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useGlobSetting } from '/@/hooks/setting';
  import { downloadByUrl } from '/@/utils/file/download';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { cusFpApplyDelete, cusFpApplyListData } from '/@/api/shop/order/cusFpApply';
  import { useDrawer } from '/@/components/Drawer';
  import { BpmRuntimeTrace } from '/@/components/Bpm';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import FormImport from './formImport.vue';
  import { useModal, BasicModal } from '/@/components/Modal';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { basUserCusTreeData } from '/@/api/bas/cus/basUserCus';
  import { basUserCusTree } from '/@/api/bas/cus/basCusKpinfo';
  import { BasicUpload } from '/@/components/Upload';

  const { t } = useI18n('shop.order.cusFpApply');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('开票申请管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('客户编码'),
        field: 'cusCode',
        component: 'Input',
      },
      {
        label: t('客户名称'),
        field: 'basCus.name',
        component: 'Input',
      },
      {
        label: t('客户简称'),
        field: 'basCus.abbName',
        component: 'Input',
      },
      {
        label: t('申请时间'),
        field: 'createDate',
        component: 'RangePicker',
      },

      {
        label: t('申请人'),
        field: 'createBy',
        component: 'Input',
      },
      {
        label: t('开票状态'),
        field: 'kpStatus',
        component: 'Select',
        componentProps: {
          dictType: 'cus_fp_status',
          allowClear: true,
        },
      },
      {
        label: t('开票完成'),
        field: 'overDate',
        component: 'RangePicker',
        componentProps: {
          format: 'YYYY-MM-DD',
          showTime: false,
        },
      },

      {
        label: t('申请备注'),
        field: 'remarks',
        component: 'Input',
      },
      {
        label: t('状态'),
        field: 'status',
        component: 'Select',
        componentProps: {
          dictType: 'bpm_biz_status',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
    ],
    fieldMapToTime: [
      ['createDate', ['createDate_gte', 'createDate_lte']],
      ['overDate', ['overDate_gte', 'overDate_lte']],
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('开票状态'),
      dataIndex: 'kpStatus',
      key: 'a.kp_status',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'cus_fp_status',
      fixed: 'left',
    },
    {
      title: t('单据号'),
      dataIndex: 'djno',
      width: 150,
      align: 'left',
    },
    {
      title: t('发票附件'),
      dataIndex: 'uploadFile',
      width: 100,
      align: 'left',
      slot: 'uploadFile',
    },
    {
      title: t('客户编码'),
      dataIndex: 'cusCode',
      key: 'a.cus_code',
      sorter: true,
      width: 120,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('客户简称'),
      dataIndex: 'basCus.abbName',
      key: 'cus.abb_name',
      sorter: true,
      width: 150,
      align: 'left',
    },
    {
      title: t('客户名称'),
      dataIndex: 'basCus.name',
      key: 'cus.name',
      sorter: true,
      width: 200,
      align: 'left',
    },
    {
      title: t('归属公司'),
      dataIndex: 'basCus.exDefine12',
      key: 'cus.ex_define12',
      sorter: true,
      width: 200,
      align: 'left',
    },

    {
      title: t('申请人'),
      dataIndex: 'creator.userName',
      key: 'a.create_by',
      sorter: true,
      width: 80,
      align: 'left',
    },
    {
      title: t('申请时间'),
      dataIndex: 'createDate',
      key: 'a.create_date',
      sorter: true,
      width: 120,
      align: 'center',
    },
    {
      title: t('申请数量'),
      dataIndex: 'iqty',
      key: 'a.iqty',
      sorter: true,
      width: 100,
      align: 'right',
    },

    {
      title: t('申请金额'),
      dataIndex: 'isum',
      key: 'a.isum',
      sorter: true,
      width: 100,
      align: 'right',
    },
    {
      title: t('折扣额'),
      dataIndex: 'isaleSum',
      key: 'a.isale_sum',
      sorter: true,
      width: 100,
      align: 'right',
    },
    {
      title: t('折扣率'),
      dataIndex: 'isaleRate',
      key: 'a.isale_rate',
      sorter: true,
      width: 100,
      align: 'right',
    },

    {
      title: t('开票人'),
      dataIndex: 'overUser.userName',
      key: 'a.over_by',
      sorter: true,
      width: 80,
      align: 'left',
    },
    {
      title: t('开票时间'),
      dataIndex: 'overDate',
      key: 'a.over_date',
      sorter: true,
      width: 120,
      align: 'center',
    },
    {
      title: t('发票链接'),
      dataIndex: 'fpUrl',
      key: 'a.fp_url',
      sorter: true,
      width: 130,
      align: 'left',
      slot: 'fpUrl',
    },
    {
      title: t('发票类型'),
      dataIndex: 'fpType',
      key: 'a.fp_type',
      sorter: true,
      width: 120,
      align: 'center',
      dictType: 'fp_type',
    },
    {
      title: t('抬头类型'),
      dataIndex: 'kpInfo.ctype',
      key: 'a.ctype',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'cus_fp_type',
    },
    {
      title: t('名称'),
      dataIndex: 'kpInfo.cname',
      key: 'a.cname',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('联系方式'),
      dataIndex: 'kpInfo.contactInfo',
      key: 'a.contact_info',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('税号'),
      dataIndex: 'kpInfo.dutyNo',
      key: 'a.duty_no',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('开户银行'),
      dataIndex: 'kpInfo.bankName',
      key: 'a.bank_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('银行帐号'),
      dataIndex: 'kpInfo.bankCode',
      key: 'a.bank_code',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('单位号码'),
      dataIndex: 'kpInfo.dwNo',
      key: 'a.dw_no',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('单位地址'),
      dataIndex: 'kpInfo.dwAddr',
      key: 'a.dw_addr',
      sorter: true,
      width: 130,
      align: 'left',
    },
    // {
    //   title: t('取消说明'),
    //   dataIndex: 'cancleRemarks',
    //   key: 'a.cancle_remarks',
    //   sorter: true,
    //   width: 180,
    //   align: 'left',
    // },
    {
      title: t('备注'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 180,
      align: 'left',
    },
    {
      title: t('审批状态'),
      dataIndex: 'status',
      key: 'a.status',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'bpm_biz_status',
      fixed: 'right',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑开票申请'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'shop:order:cusFpApply:edit',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除开票申请'),
        popConfirm: {
          title: t('是否确认删除开票申请'),
          confirm: handleDelete.bind(this, { id: record.id }),
        },
        auth: 'shop:order:cusFpApply:edit',
        ifShow: () => record.status == '9',
      },
      {
        icon: 'fluent:flowchart-20-regular',
        title: t('流程追踪'),
        onClick: handleTrace.bind(this, record),
        ifShow: () => record.status != '9',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm }] = useTable({
    api: cusFpApplyListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  function handleopen(fpUrl) {
    window.open(fpUrl, '_blank');
  }

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleExport() {
    const { ctxAdminPath } = useGlobSetting();
    downloadByUrl({
      url: ctxAdminPath + '/shop/order/cusFpApply/exportData',
      target: '_self',
    });
  }

  // 开票申请
  async function handlekpsq() {
    // openDrawer3(true, record);
    await openModalkpsq(true, {});
    resetFieldskpsq();
  }
  async function handleModalkpsq() {
    let data = await validatekpsq();
    // openDrawer(true, data);
    router.push({
      path: '/shop/order/cusFpApply/listKpSel',
      query: {
        cusCode: data.cusCode,
        cusName: data.basCus.name,
      },
    });
    closeModalkpsq();
  }

  const [registerFormkpsq, { resetFields: resetFieldskpsq, validate: validatekpsq }] = useForm({
    labelWidth: 80,
    schemas: [
      {
        label: t('客户'),
        field: 'cusCode',
        fieldLabel: 'basCus.name',
        component: 'TreeSelect',
        componentProps: {
          // api: basUserCusTree,
          api: basUserCusTreeData,
          canSelectParent: false,
          allowClear: true,
          immediate: true,
        },
        required: true,
        colProps: { lg: 24, md: 24 },
        itemProps: { validateTrigger: 'blur' },
      },
    ],
    baseColProps: { lg: 24, md: 24 },
  });

  const [registerModalkpsq, { openModal: openModalkpsq, closeModal: closeModalkpsq }] = useModal();
  const [registerImportModal, { openModal: importModal }] = useModal();

  function handleImport() {
    importModal(true, {});
  }

  async function handleDelete(record: Recordable) {
    const res = await cusFpApplyDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }

  const [registerTraceModal, { openModal: traceModel }] = useModal();

  function handleTrace(record: Recordable) {
    traceModel(true, { formKey: 'soKpApply', bizKey: record.id });
  }
</script>
